package com.x.crm.dao;

import com.x.crm.model.Warranty;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class WarrantyDao extends BaseDao {
    private final static String baseQuerySql = "SELECT * FROM CRM_WARRANTY  T1 WHERE T1.WARRANTY_ID > 0 ";
    public List<Warranty> selectWarrantyList(Warranty warranty) throws Exception {
        StringBuilder sql = new StringBuilder(baseQuerySql);
        List<Object> params = new ArrayList<>();
        if (this.isNotNull(warranty.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(warranty.getMarketId());
        }
        if (this.isNotNull(warranty.getWarrantyName())) {
            sql.append("AND T1.WARRANTY_NAME  = ? ");
            params.add(warranty.getWarrantyName());
        }
        if (this.isNotNull(warranty.getWarrantyId())) {
            sql.append("AND T1.WARRANTY_ID  = ? ");
            params.add(warranty.getWarrantyId());
        }
        if (this.isNotNull(warranty.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(warranty.getShopId());
        }
        sql.append(" ORDER BY T1.WARRANTY_CREATE_TIME DESC ");
        return this.queryForList(sql.toString(), Warranty.class, params, warranty);
    }

    /**
     * 查询条数
     * @param Warranty
     * @return
     * @throws Exception
     */
    private final static  String  selectWarrantyCountSql = "SELECT COUNT(T1.WARRANTY_ID) FROM  CRM_WARRANTY T1 WHERE 1=1 ";
    public int selectWarrantyNum(Warranty warranty) throws Exception{
        StringBuilder sql = new StringBuilder(selectWarrantyCountSql);
        List<Object> params = new ArrayList<Object>();
        if (this.isNotNull(warranty.getMarketId())) {
            sql.append("AND T1.MARKET_ID = ? ");
            params.add(warranty.getMarketId());
        }
        if (this.isNotNull(warranty.getWarrantyName())) {
            sql.append("AND T1.WARRANTY_NAME  = ? ");
            params.add(warranty.getWarrantyName());
        }
        if (this.isNotNull(warranty.getShopId())) {
            sql.append("AND T1.SHOP_ID  = ? ");
            params.add(warranty.getShopId());
        }
        return this.queryForInt(sql.toString(), params);
    }

}